package com.example.guesswho.ui.components

import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import kotlinx.coroutines.delay

@Composable
fun CountdownTimer(
    totalTime: Int = 30,
    timeLeft: Int = totalTime,
    onTimeUp: () -> Unit,
    modifier: Modifier = Modifier
) {
    var currentTimeLeft by remember { mutableStateOf(timeLeft) }
    var isRunning by remember { mutableStateOf(true) }

    LaunchedEffect(isRunning, timeLeft) {
        if (isRunning && currentTimeLeft > 0) {
            delay(1000)
            currentTimeLeft--
            if (currentTimeLeft == 0) {
                isRunning = false
                onTimeUp()
            }
        }
    }

    Card(
        modifier = modifier,
        colors = CardDefaults.cardColors(
            containerColor = when {
                timeLeft <= 5 -> MaterialTheme.colorScheme.errorContainer
                timeLeft <= 10 -> MaterialTheme.colorScheme.warningContainer
                else -> MaterialTheme.colorScheme.primaryContainer
            }
        )
    ) {
        Box(
            modifier = Modifier
                .size(80.dp)
                .padding(16.dp),
            contentAlignment = Alignment.Center
        ) {
            Text(
                text = "${currentTimeLeft}s",
                fontSize = 24.sp,
                fontWeight = FontWeight.Bold,
                color = when {
                    currentTimeLeft <= 5 -> MaterialTheme.colorScheme.onErrorContainer
                    currentTimeLeft <= 10 -> MaterialTheme.colorScheme.onWarningContainer
                    else -> MaterialTheme.colorScheme.onPrimaryContainer
                }
            )
        }
    }
}

@Composable
fun PrimaryButton(
    text: String,
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
    enabled: Boolean = true
) {
    Button(
        onClick = onClick,
        modifier = modifier
            .fillMaxWidth()
            .height(56.dp),
        enabled = enabled,
        colors = ButtonDefaults.buttonColors(
            containerColor = MaterialTheme.colorScheme.primary,
            contentColor = MaterialTheme.colorScheme.onPrimary
        )
    ) {
        Text(
            text = text,
            fontSize = 18.sp,
            fontWeight = FontWeight.Medium
        )
    }
}

@Composable
fun SecondaryButton(
    text: String,
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
    enabled: Boolean = true
) {
    OutlinedButton(
        onClick = onClick,
        modifier = modifier
            .fillMaxWidth()
            .height(56.dp),
        enabled = enabled,
        colors = ButtonDefaults.outlinedButtonColors(
            contentColor = MaterialTheme.colorScheme.primary
        )
    ) {
        Text(
            text = text,
            fontSize = 18.sp,
            fontWeight = FontWeight.Medium
        )
    }
}